(* Utility routines *)

open Lexing

(* Lexing.position -> string *)

let string_of_pos (p : Lexing.position) =
  let column = p.Lexing.pos_cnum - p.Lexing.pos_bol in
  Printf.sprintf "file %s, line %d column %d"
    p.pos_fname p.pos_lnum column


(* Initialize a lex buffer with information from the current
   file *)

let from_channel ch file =
  let lexbuf = Lexing.from_channel ch in
  let op = lexbuf.lex_curr_p in
  let np = { pos_fname = file
	   ; pos_lnum = op.pos_lnum
	   ; pos_bol = op.pos_bol
	   ; pos_cnum = op.pos_cnum }
  in
  lexbuf.lex_curr_p <- np;
  lexbuf


(* Update the line position of a lex buffer *)

let incr_linenum lexbuf =
  let pos = lexbuf.lex_curr_p in
  lexbuf.lex_curr_p <-
    { pos with
      pos_lnum = pos.pos_lnum + 1;
      pos_bol  = pos.pos_cnum
    }
